package com.ocean;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ocean.common.enums.Globals;

import java.rmi.ServerException;

public class Smsdx {

    /**
     * 短信发送
     * @param accessKeyId
     * @param accessSecret
     * @param regionid
     * @param phoneNumbers
     * @param signName
     * @param templateCode
     * @param templateParam
     * @return
     */
    public static boolean SendSms(String accessKeyId, String accessSecret, String regionid, String phoneNumbers, String signName, String templateCode, String templateParam){
        DefaultProfile profile = DefaultProfile.getProfile(regionid, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", regionid);
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            //this.addAutoLog("短信发送失败,失败原因：" + e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR);
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        String templateParam = "{\"productname\":\""+ "产品名称" + "\",\"filename\":\"" + "文件名称" + "\",\"DLtime\":\"" + "多少天" + "天\"}";

        SendSms("LTAIqcYijk60jZcz","pt3jHb1tTwPvK0uh42VQIwjLASu3h6","cn-hangzhou","15117910426","国家海洋预报云服务平台","SMS_168590984",templateParam);

    }
}
